const { query } = require('../db/mysql')

const blogs = {
  getList (author, keyword) {
    let sql = `select * from blogs where 1=1 `
    if (author) {
      sql += `and author='${author}' `
    }
    if (keyword) {
      sql += `and title like '%${keyword}%' `
    }
    sql += `order by createtime desc;`

    return query(sql)
  },
  getDetail (id) {
    const sql = `select * from blogs where id='${id}'`
    return query(sql)
  },
  newBlog (title, content, author, createTime) {
    const sql = `insert into blogs (title, content, createtime, author) values ('${title}', '${content}', ${createTime}, '${author}')`
    return query(sql)
  },
  updateBlog (id, title, content) {
    const sql = `update blogs set title='${title}', content='${content}' where id='${id}'`
    return query(sql)
  },
  delBlog (id, author) {
    const sql = `delete from blogs where id='${id}' and author='${author}'`
    return query(sql)
  }
}
module.exports = blogs
